package com.code.classsystem.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.code.classsystem.common.shiro.util.ShiroUtils;
import com.code.classsystem.dao.ClassUserMapper;
import com.code.classsystem.entity.ClassCourse;
import com.code.classsystem.entity.ClassUser;
import com.code.classsystem.entity.Paper;
import com.code.classsystem.dao.PaperMapper;
import com.code.classsystem.service.ClassCourseService;
import com.code.classsystem.service.PaperQuestionService;
import com.code.classsystem.service.PaperService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.code.classsystem.vo.PaperInfoVo;
import com.code.classsystem.vo.PaperResultBinVo;
import com.code.classsystem.vo.StudentPaperRateVo;
import com.code.core.util.DateUtils;
import com.code.core.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author coder
 * @since 2020-04-18
 */
@Service
public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper> implements PaperService {

    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private PaperQuestionService paperQuestionService;
    @Autowired
    private ClassCourseService classCourseService;


    @Override
    public boolean createPaper(Paper paper) {

        String cId = paper.getCourseId();
        String clId = paper.getClassId();
        List<ClassCourse> classCourseList = classCourseService.iid(clId,cId);
        if (classCourseList.size()==0){
            return false;
        }
        paper.setClassId(classCourseList.get(0).getClassId());
        paper.setCourseId(classCourseList.get(0).getCourseId());
        String id= UUIDUtil.getUUid();
        String userId=ShiroUtils.getUserId();
        String curTime= DateUtils.getTimeStr("yyyy-mm-dd hh:mm:ss");
        paper.setPublishTime(curTime);
        paper.setId(id);
        paper.setPublishUserId(userId);
        return this.insert(paper);
    }

    @Override
    public List<Paper> queryPaper(Paper paper, int page, int limit) {
        Page<Paper> paperPage=new Page<>(page,limit);
        EntityWrapper<Paper> wrapper=new EntityWrapper<>();
        wrapper.eq("course_id",paper.getCourseId());
        wrapper.orderBy("publish_time",false);
        List<Paper>list=this.selectPage(paperPage,wrapper).getRecords();
        return list;
    }

    @Override
    public PageInfo<PaperInfoVo> listPage(Paper paper,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        paper.setPublishUserId(ShiroUtils.getUserId());
        List<PaperInfoVo> paperInfoVoList=paperMapper.listPage(paper);
        return new PageInfo<>(paperInfoVoList);
    }

    @Override
    public List<Paper> queryAllPaper(int pageNum, int pageSize) {
        Page<Paper> paperPage=new Page<>(pageNum,pageSize);
        List<Paper>list=this.selectPage(paperPage,null).getRecords();
        return list;
    }


    @Override
    public List<Paper> queryMyPaper(int pageNum, int pageSize) {
        String userId=ShiroUtils.getUserId();
        PageHelper.startPage(pageNum, pageSize);
        List<Paper>papers=paperMapper.queryMyPaper(userId);
        return new PageInfo<Paper>(papers).getList();
    }

    @Override
    public List<PaperInfoVo> selectByUserId(String userId){
        List<PaperInfoVo> paperInfoVos = paperMapper.selectByUserId(userId);

        return paperInfoVos;
    }

    @Override
    public Map<String, Integer> queryPaperResult(String courseId) {
        Map<String, Integer> t = new HashMap<>();
        t.put("优秀（90-100）",0);
        t.put("一般（60-90）",0);
        t.put("良好（0-60）",0);
        List<Double> paperResultBinVos= paperMapper.queryPaperResult(courseId);

        for(int i = 0; i < paperResultBinVos.size(); i++){
            if (paperResultBinVos.get(i)>=0.9){
                t.put("优秀（90-100）",t.get("优秀（90-100）")+1);
            }else if (paperResultBinVos.get(i)>=0.6){
                t.put("一般（60-90）",t.get("一般（60-90）")+1);
            }else{
                t.put("良好（0-60）",t.get("良好（0-60）")+1);
            }
        }
        return t;
//        double total_rate=0.0;
//        DecimalFormat df = new DecimalFormat("#.00");
//        for(int i=0;i<paperResultBinVos.size();i++){
//            double rate=Double.parseDouble(paperResultBinVos.get(i).getGoodRate());
//            total_rate+=rate;
//        }
//        for(int i=0;i<paperResultBinVos.size();i++){
//            double rate2=Double.parseDouble(paperResultBinVos.get(i).getGoodRate());
//            String result=df.format(rate2/total_rate);
//            paperResultBinVos.get(i).setGoodRate(result);
//        }
//        return paperResultBinVos;

    }

    @Override
    public Map<String, Map<String, Integer>> queryPaperResult2(String courseId){
        List<StudentPaperRateVo> studentPaperRateVoList= paperMapper.queryPaperResult2("0358c5b8-713e-45d7-bfba-ba687a7bddf4");
        Map<String, Double> stringListMap = new HashMap<>();
        //分类
        Map<String, List<Double>> collect = studentPaperRateVoList.stream().collect(
                Collectors.groupingBy(
                        //设置分类关键字
                        StudentPaperRateVo::getCourseName,
                        //自定义分类结果
                        Collectors.mapping(StudentPaperRateVo::getRate, Collectors.toList())
                )
        );
        //统计
        Map<String, Map<String, Integer>> r = new HashMap<>();
        for (String item : collect.keySet()){
            Map<String, Integer> t = new HashMap<>();
            t.put("优秀（90-100）",0);
            t.put("一般（60-90）",0);
            t.put("良好（0-60）",0);


            for(int i = 0; i < collect.get(item).size(); i++){
                if (collect.get(item).get(i)>=0.9){
                    t.put("优秀（90-100）",t.get("优秀（90-100）")+1);
                }else if (collect.get(item).get(i)>=0.6){
                    t.put("一般（60-90）",t.get("一般（60-90）")+1);
                }else{
                    t.put("良好（0-60）",t.get("良好（0-60）")+1);
                }
            }
            r.put(item, t);
        }

        return r;
    }


    @Override
    public boolean delPaper(String paperId) {
        paperQuestionService.delPaper(paperId);
        paperMapper.deleteById(paperId);
        return true;
    }
}
